package com.ptu.controller.user;

import com.ptu.dto.OrderDTO;
import com.ptu.entity.Order;
import com.ptu.server.OrderServer;
import com.ptu.util.Result;
import com.ptu.vo.OrderVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("userOrderController")
@Tag(name = "订单模块")
@RequestMapping("/order")
public class OrderController {

    @Autowired
    OrderServer orderServer;

    //添加订单
    @Operation(summary = "添加订单")
    @PostMapping("/add")
    public Result<Order> addOrder(@RequestBody OrderDTO orderDTO) {
        System.out.println("添加订单");
        System.out.println(orderDTO);
        Order order= null;
        try {
            order = orderServer.addOrder(orderDTO);
        } catch (Exception e) {
            String errorMessage = e.getMessage();
            System.out.println("错误消息: " + errorMessage);
            return Result.error(errorMessage);
        }
        return Result.success(order);
    }

    @Operation(summary = "获取订单列表")
    @GetMapping("/list")
    public List<OrderVO> list() {
        System.out.println("获取订单列表");
        List<OrderVO> list = orderServer.list();
        return list;
    }

    @Operation(summary = "获取所有已评论订单列表")
    @GetMapping("/allList")
    public List<OrderVO> allList() {
        System.out.println("获取订单列表");
        List<OrderVO> list = orderServer.allList();
        return list;
    }

    @Operation(summary = "获取订单详情")
    @PostMapping("/detail")
    public Result<Order> detail(Long id) {
        System.out.println("获取订单详情" + id);
        Order detail = orderServer.detail(id);
        return Result.success(detail);
    }

//    @Operation(summary = "支付订单")
//    @PostMapping("/pay")
//    public Result<Void> pay(Long id) {
//        System.out.println("支付订单");
//        orderServer.pay(id);
//        return Result.success();
//    }

    @Operation(summary = "取消订单")
    @PostMapping("/cancel")
    public Result<Void> cancel(Long id) {
        System.out.println("取消订单");
        orderServer.cancel(id);
        return Result.success();
    }

    @Operation(summary = "确认收货")
    @PostMapping("/confirm")
    public Result<Void> confirm(Long id) {
        System.out.println("确认收货");
        orderServer.confirm(id);
        return Result.success();
    }

    @Operation(summary = "退货")
    @PostMapping("/return")
    public Result<Void> returnGoods(Long id) {
        System.out.println("退货");
        orderServer.returnGoods(id);
        return Result.success();
    }

    @Operation(summary = "发货")
    @PostMapping("/deliver")
    public Result<Void> deliver(Long id) {
        System.out.println("发货");
        orderServer.deliver(id);
        return Result.success();
    }

    @Operation(summary = "用户评价订单")
    @PostMapping("/comment")
    public Result<Void> comment(Long id) {
        System.out.println("用户评价订单");
        orderServer.comment(id);
        return Result.success();
    }




}
